<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        border-collapse: collapse;
      }

      td {
        border: 1px solid #000;
        padding: 8px 12px;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <td>1-1</td>
        <td>2-1</td>
        <td>3-1</td>
        <td>4-1</td>
      </tr>
      <tr>
        <td>1-2</td>
        <td>2-2</td>
        <td>3-2</td>
        <td>4-2</td>
      </tr>
      <tr>
        <td>1-3</td>
        <td>2-3</td>
        <td>3-3</td>
        <td>4-3</td>
      </tr>
      <tr>
        <td>1-4</td>
        <td>2-4</td>
        <td>3-4</td>
        <td>4-4</td>
      </tr>
    </table>

    <script>
      var tableEl = document.body.firstElementChild;

      // // 1.获取1-1
      // var row1El = tableEl.rows[0]
      // var cell1El = row1El.cells[0]
      // console.log(cell1El)

      // // 2.获取2-2
      // var row2El = tableEl.rows[1]
      // var cell2El = row1El.cells[1]

      // for循环
      for (var i = 0; i < tableEl.rows.length; i++) {
        var rowEl = tableEl.rows[i];
        var cellEl = rowEl.cells[i];

        // 设置样式
        cellEl.style.backgroundColor = "red";
        cellEl.style.color = "white";
      }
    </script>
  </body>
</html>
